package com.suptrack.dao.jpa;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;

import com.suptrack.dao.ZoneDao;
import com.suptrack.entity.Zone;

@Stateless
public class JpaZoneDao implements ZoneDao {
	
	@PersistenceContext(unitName="Suptracking")
    private EntityManager em;

	@Override
	public void addZone(Zone zone) {
		try {
			
			em.persist(zone);
			

		}

		finally {
			
			

		}
		
	}

	@Override
	public void deleteZonebyId(long id) {


		try {
			

			Zone zonetoremove = getZoneInfo(id);
			zonetoremove = em.merge(zonetoremove);

			em.remove(zonetoremove);
			

		}

		finally {
			
			
		}
		
	}

	@Override
	public Zone getZoneInfo(long _id) {
		Zone return_zone;
		
		try {
			
			return_zone = em.find(Zone.class, _id);
			

		}

		finally {
			
			
		}
		
		return return_zone;
	}

	@Override
	public List<Zone> getAllZone() {
		CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Zone> query = cb.createQuery(Zone.class);
        List<Zone> All_Zone = em.createQuery(query).getResultList();		
		return All_Zone;
	}

	@Override
	public void deleteZone(Zone _zone) {
		em.remove(em.find(Zone.class,_zone.getId()));
		
	}

}
